package com.hdyg.sixg.activity.home;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hdyg.sixg.R;
import com.hdyg.sixg.activity.common.BaseActivity;
import com.hdyg.sixg.adapter.ImageAdapter;
import com.hdyg.sixg.entry.ImageBean;
import com.hdyg.sixg.entry.RechargeBean;
import com.hdyg.sixg.mvp.Contrant.CRecharge;
import com.hdyg.sixg.mvp.base.BasePresenter;
import com.hdyg.sixg.mvp.base.BaseUrl;
import com.hdyg.sixg.mvp.base.GetParamUtil;
import com.hdyg.sixg.mvp.presenter.PRecharge;
import com.hdyg.sixg.util.FileSizeUtil;
import com.hdyg.sixg.util.ImageLoadUtil;
import com.hdyg.sixg.util.LogUtils;
import com.hdyg.sixg.util.StringUtil;
import com.hdyg.sixg.util.TakePhotoUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * @author CZB
 * @describe 充值
 * @time 2019/4/3 16:51
 */
public class RechargeActivity extends BaseActivity implements CRecharge.IVRecharge {

    @BindView(R.id.ll_top_left)
    LinearLayout llTopLeft;
    @BindView(R.id.tvTopTitle)
    TextView tvTopTitle;
    @BindView(R.id.tv_top_right)
    TextView tvTopRight;
    @BindView(R.id.ll_top_right)
    LinearLayout llTopRight;
    @BindView(R.id.iv_qr_code)
    ImageView ivQrCode;
    @BindView(R.id.tv_address)
    TextView tvAddress;
    @BindView(R.id.bt_copy)
    Button btCopy;
    @BindView(R.id.rv_image)
    RecyclerView rvImage;
    @BindView(R.id.bt_sure)
    Button btSure;
    @BindView(R.id.srl_refresh)
    SmartRefreshLayout srlRefresh;

    private ImageAdapter imageAdapter;
    private List<String> imageBeanList;
    private CRecharge.IPRecharge mPresenter;
    private String picture;//多张图片用，号隔开
    private RechargeBean bean;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_recharge;
    }

    @Override
    protected void initView() {
        onClickListener();
        tvTopTitle.setText(R.string.recharge_title);
//        tvTopRight.setText(R.string.recharge_right);
        rvImage.setLayoutManager(new GridLayoutManager(mContext, 6, GridLayoutManager.VERTICAL, false));
        imageBeanList = new ArrayList<>();
        initAdapter();
    }

    @Override
    protected void initData() {
        mPresenter.pGetInit(BaseUrl.RECHARGE_URL,GetParamUtil.getRechargeParam());
    }

    @Override
    protected void createPresenter() {
        mPresenter = new PRecharge(this);
    }

    private void onClickListener() {
        srlRefresh.setOnRefreshListener(refreshLayout -> {
            srlRefresh.finishRefresh(2000);//传入false表示刷新失败
            initData();
        });
        btCopy.setOnClickListener(v -> {
            if (StringUtil.isFastDoubleClick()){
                return;
            }
            StringUtil.copyTextMethod(mContext,bean.getWallet_token());
        });
        btSure.setOnClickListener(v -> {
            List<ImageBean> list = imageAdapter.getDatas();
            if (list != null && list.size() != 0) {
                StringBuffer buffer1 = new StringBuffer();
                for (int i = 0; i < list.size(); i++) {
                    buffer1.append(list.get(i).getSrc()).append(",");
                }
                picture = buffer1.substring(0, buffer1.length() - 1);
            }
            if (TextUtils.isEmpty(picture)){
                toastNotifyShort(R.string.toast_image);
                return;
            }
            if (StringUtil.isFastDoubleClick()){
                return;
            }
            mPresenter.pGetApplyRecharge(BaseUrl.APPLY_RECHARGE_URL,GetParamUtil.getApplyRechargeParam(picture));
        });
//        llTopRight.setOnClickListener(v ->
//            startActivity(RechargeRecordActivity.class));
        llTopLeft.setOnClickListener(v -> {
            finish();
        });
    }

    private void initAdapter() {
        imageAdapter = new ImageAdapter(mContext, imageBeanList);
        rvImage.setAdapter(imageAdapter);
        //选择图片
        imageAdapter.setOnImgSelectClickListener(() -> {
            int size = imageAdapter.getImageDatas();
            if (size >= ImageAdapter.IMAGE_MAX_SIZE) {
                return;
            }
            TakePhotoUtil.getInstance().sustomPhotoMult(mContext, ImageAdapter.IMAGE_MAX_SIZE - size, list -> {
                for (int i = 0; i < list.size(); i++) {
                    double imgSize = FileSizeUtil.getFileOrFilesSize(list.get(i).getOriginalPath(), FileSizeUtil.SIZETYPE_MB);//图片大小
                    LogUtils.e("图片大小==>" + imgSize);
                    //显示图片小于2M才能上传
                    if (imgSize <= BaseUrl.IMG_SIZE_LIMIT) {
                        File file = new File(list.get(i).getOriginalPath());
                        mPresenter.pGetUploadImg(BaseUrl.UPLOAD_URL,file,"picture",GetParamUtil.getUploadImageParam());
                    } else {
                        String str = String.format(getResources().getString(R.string.toast_image_size),String.valueOf(BaseUrl.IMG_SIZE_LIMIT));
                        toastNotifyShort(str);
                    }
                }
            });
        });
        //删除图片
        imageAdapter.setOnImgDelClickListener(pos -> {
            imageAdapter.removeData(pos);
        });
    }

    @Override
    public void vGetInitSuccess(RechargeBean dataBean) {
        bean = dataBean;
        ImageLoadUtil.imageLoad(mContext,bean.getQrCode(),ivQrCode);
        tvAddress.setText(bean.getWallet_token());
    }

    @Override
    public void vGetApplyRechargeSuccess(String dataBean) {
        toastNotifyShort(dataBean);
        setResult(RESULT_OK);
        finish();
    }

    @Override
    public void vGetUploadImgSuccess(ImageBean dataBean) {
        imageAdapter.addData(dataBean);
    }
}
